﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using park.cn.linkit.epark;


namespace park.Location
{
    public partial class proxyforcity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["m"] != null)
            {
                string m = Request.Params["m"];
                switch (m)
                {
                    case "ListCityAreaStreet":
                        ListCityAreaStreet();
                        break;
                    case "AddCity":
                        AddCity();
                        break;
                    case "AddArea":
                        AddArea();
                        break;
                    case "AddStreet":
                        AddStreet();
                        break;
                    case "EditArea":
                        EditArea();
                        break;
                    case "EditCity":
                        EditCity();
                        break;
                    case "EditStreet":
                        EditStreet();
                        break;
                    case "Delete":
                        Delete();
                        break;
                }
            }
        }

        void ListCityAreaStreet()
        {
            string json = @"{""cityList"":[{""id"":123,""name"":""汉口"",""order"":1}],""areaList"":[{""id"":234,""name"":""硚口"",""pid"":123,""order"":1}],""streetList"":[{""id"":456,""name"":""太平洋街"",""pid"":234,""order"":1}]}";
            json = new ServiceList().ListCityAreaStreet();
            Response.Write("<script>parent.BindList("+json+");</script>");
            Response.End();
        }

        void AddArea()
        {
            bool ret = true;
            string name = Request.Params["name"];
            string order = Request.Params["order"];
            string city_id = Request.Params["ddl_City"];
            byte[] filebytes = null;
            if (Request.Files.Count > 0)
            {
                HttpPostedFile file = Request.Files[0];
                filebytes = new byte[file.ContentLength];
                file.InputStream.Read(filebytes, 0, file.ContentLength);
            }
            ret = new ServiceList().AddArea(name, city_id, order, filebytes);
            if (ret)
            {
                Response.Write("<script>alert('添加成功');parent.GotoListPage();</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败');</script>");
            }
            Response.End();
        }

        void AddCity()
        {
            bool ret = true;
            string name = Request.Params["name"];
            string order = Request.Params["order"];
            byte[] filebytes = null;
            if (Request.Files.Count > 0)
            {
                HttpPostedFile file = Request.Files[0];
                filebytes = new byte[file.ContentLength];
                file.InputStream.Read(filebytes, 0, file.ContentLength);
            }
            ret = new ServiceList().AddCity(name, order, filebytes);
            if (ret)
            {
                Response.Write("<script>alert('添加成功');;parent.GotoListPage();</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败');</script>");
            }
            Response.End();
        }

        void AddStreet()
        {
            bool ret = true;
            string name = Request.Params["name"];
            string order = Request.Params["order"];
            string area_id = Request.Params["ddl_Area"];
            ret = new ServiceList().AddStreet(name, area_id, order);
            if (ret)
            {
                Response.Write("<script>alert('添加成功');;parent.GotoListPage();</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败');</script>");
            }
            Response.End();
        }

        void EditArea()
        {
            bool isInit = Request.Params["init"] == "1";
            string id = Request.Params["id"];
            if (isInit)
            {
                string json = new ServiceList().GetAreaInfo(id);
                Response.Write("<script>;parent.InitEditAreaPage(" + json + ");</script>");
            }
            else
            {
                string order = Request.Params["order"];
                byte[] filebytes = null;
                if (Request.Files.Count > 0)
                {
                    HttpPostedFile file = Request.Files[0];
                    filebytes = new byte[file.ContentLength];
                    file.InputStream.Read(filebytes, 0, file.ContentLength);
                }
                bool ret = true;
                ret = new ServiceList().EditArea(id, order, filebytes);
                if (ret)
                {
                    Response.Write("<script>alert('修改成功');;parent.GotoListPage();</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败');</script>");
                }
            }
            Response.End();
        }

        void EditCity()
        {
            bool isInit = Request.Params["init"] == "1";
            string id = Request.Params["id"];
            if (isInit)
            {
                string json = new ServiceList().GetCityInfo(id);
                Response.Write("<script>;parent.InitEditCityPage(" + json + ");</script>");
            }
            else
            {
                string order = Request.Params["order"];
                byte[] filebytes = null;
                if (Request.Files.Count > 0)
                {
                    HttpPostedFile file = Request.Files[0];
                    filebytes = new byte[file.ContentLength];
                    file.InputStream.Read(filebytes, 0, file.ContentLength);
                }
                bool ret = true;
                ret = new ServiceList().EditCity(id, order, filebytes);
                if (ret)
                {
                    Response.Write("<script>alert('修改成功');;parent.GotoListPage();</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败');</script>");
                }
            }
            Response.End();
        }

        void EditStreet()
        {
            bool isInit = Request.Params["init"] == "1";
            string id = Request.Params["id"];
            if (isInit)
            {
                string json = new ServiceList().GetStreetInfo(id);
                Response.Write("<script>;parent.InitEditStreetPage(" + json + ");</script>");
            }
            else
            {
                string order = Request.Params["order"];                
                bool ret = true;
                ret = new ServiceList().EditStreet(id, order);
                if (ret)
                {
                    Response.Write("<script>alert('修改成功');;parent.GotoListPage();</script>");
                }
                else
                {
                    Response.Write("<script>alert('修改失败');</script>");
                }
            }
            Response.End();
        }

        void Delete()
        {
            string id = Request.Params["id"];
            string type = Request.Params["type"];
            bool ret = true;
            switch (type)
            {
                case "c":
                    ret = new ServiceList().DeleteCity(id);
                    break;
                case "a":
                    ret = new ServiceList().DeleteArea(id);
                    break;
                case "s":
                    ret = new ServiceList().DeleteStreet(id);
                    break;
            }
            if (ret)
            {
                Response.Write("alert('删除成功');;GotoListPage();");
            }
            else
            {
                Response.Write("alert('删除失败');");
            }
            Response.End();
        }
    }
}